/**
 * 
 */
package nc.bs.mlr.querydmo;

import java.util.List;

import javax.naming.NamingException;

import nc.bs.dao.BaseDAO;
import nc.bs.pub.DataManageObject;
import nc.bs.pub.pf.IQueryData;
import nc.bs.pub.pf.IQueryData2;
import nc.bs.pub.pf.ISaveAfterBusi;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.vo.mlr.jgd.PortbillBVO;
import nc.vo.mlr.jgd.PortbillHVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.SuperVO;
import nc.vo.pub.pf.PfPOArriveVO;

/**
 * @author w_y_x
 *
 */
public class JGDSourceQueryDMO extends DataManageObject implements ISaveAfterBusi,IQueryData,IQueryData2 {
	
	protected SuperVO headvo = new PortbillHVO();
	protected SuperVO bodyvo = new PortbillBVO();
	
	String headitemwherepart=" 1=1 ";
	BaseDAO dao=null;
	public BaseDAO getDao() {
		
		if(dao==null){
			dao=new BaseDAO();
		}
		return dao;
	}

	/**
	 * @throws NamingException
	 */
	public JGDSourceQueryDMO() throws NamingException {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param dbName
	 * @throws NamingException
	 */
	public JGDSourceQueryDMO(String dbName) throws NamingException {
		super(dbName);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see nc.bs.pub.pf.ISaveAfterBusi#affectBusi(nc.vo.pub.pf.PfPOArriveVO)
	 */
	public void affectBusi(PfPOArriveVO vo) throws BusinessException {
		// TODO Auto-generated method stub
    
	}

	/* (non-Javadoc)
	 * @see nc.bs.pub.pf.ISaveAfterBusi#getRelaBillType()
	 */
	public String getRelaBillType() {
		// TODO Auto-generated method stub
		return null;
	}

	public CircularlyAccessibleValueObject[] queryAllBodyData(String key)
			throws BusinessException {  
		CircularlyAccessibleValueObject[] vos = null;
		BaseDAO dao= getDao(); 
		String headTable = headvo.getTableName();
		String headPK=headvo.getPKFieldName();
		String bodyTable = bodyvo.getTableName();
		String headjoinPK=bodyvo.getParentPKFieldName();
		String bodyPK=bodyvo.getPKFieldName();
		String sql="select "+bodyTable+".* from "+bodyTable+"  where "+bodyTable+"."+bodyPK+" in (" +
				" select distinct " +bodyTable+"."+bodyPK+" from "+bodyTable+" left join (select "+headTable+".* from "+headTable+" where nvl(dr,0)=0 ) "+headTable+
				" on " +headTable+"."+headPK+" = "+bodyTable+"."+headjoinPK+
				" where nvl("+bodyTable+".dr,0)=0  and "+bodyTable+"."+headjoinPK+"= '"+key+"' "+
				" and " +headitemwherepart+
				")";
		 
		List ls =   (List) dao.executeQuery(sql.toString(), new BeanListProcessor(bodyvo.getClass()));
		 
		if(ls!=null&&ls.size()>0){
			vos=(CircularlyAccessibleValueObject[]) ls.toArray(new CircularlyAccessibleValueObject[0]);
		}
		 
		return  vos ;
	
	}

	public CircularlyAccessibleValueObject[] queryAllHeadData(String whereString)
			throws BusinessException { 
		CircularlyAccessibleValueObject[] vos = null;
		String headTable = headvo.getTableName();
		String headPK=headvo.getPKFieldName();
		String bodyTable = bodyvo.getTableName();
		String headjoinPK=bodyvo.getParentPKFieldName();
		String sql="select "+headTable+".* from "+headTable+"  where "+headTable+"."+headPK+" in (" +
				" select distinct " +headTable+"."+headPK+" from "+headTable+" left join (select "+bodyTable+".* from "+bodyTable+" where nvl(dr,0)=0 ) "+bodyTable+
				" on " +headTable+"."+headPK+" = "+bodyTable+"."+headjoinPK+
				" where nvl("+headTable+".dr,0)=0 " +
				" and " +whereString+
				")";
		
		BaseDAO dao= getDao();
		List ls =   (List) dao.executeQuery(sql.toString(), new BeanListProcessor(headvo.getClass()));
 
		if(ls!=null&&ls.size()>0){
			vos=(CircularlyAccessibleValueObject[]) ls.toArray(new CircularlyAccessibleValueObject[0]);
		}
		headitemwherepart= whereString;
		return  vos ;
	}

	public CircularlyAccessibleValueObject[] queryAllBodyData(String key,
			String whereString) throws BusinessException {
		// TODO Auto-generated method stub
		CircularlyAccessibleValueObject[] vos = null;
		BaseDAO dao= getDao(); 
		String headTable = headvo.getTableName();
		String headPK=headvo.getPKFieldName();
		String bodyTable = bodyvo.getTableName();
		String headjoinPK=bodyvo.getParentPKFieldName();
		String bodyPK=bodyvo.getPKFieldName();
		String sql="select "+bodyTable+".* from "+bodyTable+"  where "+bodyTable+"."+bodyPK+" in (" +
				" select distinct " +bodyTable+"."+bodyPK+" from "+bodyTable+" left join (select "+headTable+".* from "+headTable+" where nvl(dr,0)=0 ) "+headTable+
				" on " +headTable+"."+headPK+" = "+bodyTable+"."+headjoinPK+
				" where nvl("+bodyTable+".dr,0)=0  and "+bodyTable+"."+headjoinPK+"= '"+key+"' "+
				" and " +headitemwherepart+(whereString==null?"":" and "+whereString)+
				")";
		
		
		List ls =   (List) dao.executeQuery(sql.toString(), new BeanListProcessor(bodyvo.getClass()));
		 
		if(ls!=null&&ls.size()>0){
			vos=(CircularlyAccessibleValueObject[]) ls.toArray(new CircularlyAccessibleValueObject[0]);
		}
		 
		return  vos ;
	}

}
